*Put directory path here, and put data here. Create a subfolder called "Output".
cd "Drive:\Folder"

****Daughters
{
*Need a birth year data set
use "MainRHS.dta", clear
keep lopenr Byr yr_str_exog_swap
save Birthyr, replace
**********************************************************************************
*Count GP diagnoses; binary mental health diagnoses; count contraception visit
forvalues yr=2006/2014{
use "KUHR`yr'.dta" 
merge m:1 lopenr using Birthyr //merge in birth year to determine age
drop _merge
keep if Byr == `yr' - 15
gen DxMentalHealth15 = (upper(substr(Diagnose,1,1)) == "P") //Diagnoses for mental health = All starting with P
gen ReproBinary15 = inlist(Diagnose,"W10","W11","W12","W14") //Diagnoses for BC, binary
by lopenr Diagnose, sort: gen nvals = _n == 1 //tag first lopenr X Dx combo
by lopenr: replace nvals = sum(nvals) //count up and replace for all obs
by lopenr: replace nvals = nvals[_N] 
rename nvals DxCt15
*Collapse; max for binary, mean already counted, count bc =1 if BC visit
collapse (max) DxMentalHealth15 ReproBinary15 (mean) DxCt15, by(lopenr) 
replace DxMentalHealth15 = 0 if DxMentalHealth15==. //0s for people with no MH diag
replace DxCt15 = 0 if DxCt15==. //0s for people with no diag
replace ReproBinary15 = 0 if ReproBinary15==. //0s for people with BC visit
*Append if not the first year of data
if `yr' > 2006 append using Dx 
save Dx, replace
}
label var DxMentalHealth15 "Any mental health Dx at age 15"
label var DxCt15 "Total unique Dx at age 15"
label var ReproBinary15 "Any birth control Dx (visits)at age 15"
*Keep needed variables
keep DxMentalHealth15 DxCt15 ReproBinary15 lopenr
**Save
save HealthOutcomes.dta, replace
**********************************************************************************
*Fertility at age 15
use "som2008_2014.dta"
keep if regexm(hoved1,"Z37") | regexm(hoved2,"Z37") | ///
	regexm(hoved1,"Z38") | regexm(hoved2,"Z38") //only deliveries
// source: ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Publications/ICD10CM/2020/
gen Fertility15 = 1
collapse (mean) Fertility15, by(lopenr aar)
merge m:1 lopenr using Birthyr //merge in birth year to determine age
drop _merge 
*Keep only needed years
keep if Byr == aar - 15 
*Merge birthyr again to create observation even wo fertility at age 15
merge m:1 lopenr using Birthyr 
drop _merge
replace Fertility15 = 0 if Fertility15==. //0s for people with no admit data
label var Fertility15 "Fertility (admission for delivery) at age 15"
*Keep needed variables
keep Fertility15 lopenr
*Merge to other health outcomes
merge 1:1 lopenr using HealthOutcomes.dta
drop _merge
*Save
save HealthOutcomes.dta, replace
}

****Mothers
{
*Need a birth year data set
use "MainRHS.dta", clear
keep Byr yr_str_exog_swap lopenr_mor lopenr
save Birthyr, replace

**********************************************************************************
*Count GP diagnoses; binary mental health diagnoses; count contraception visit
forvalues yr=2006/2014{
use "KUHR`yr'.dta", clear 
rename lopenr lopenr_mor
gen DxMentalHealth15 = (upper(substr(Diagnose,1,1)) == "P") //Diagnoses for mental health = All starting with P
gen ReproBinary15 = inlist(Diagnose,"W10","W11","W12","W14") //Diagnoses for BC, binary
by lopenr_mor Diagnose, sort: gen nvals = _n == 1 //tag first lopenr X Dx combo
by lopenr_mor: replace nvals = sum(nvals) //count up and replace for all obs
by lopenr_mor: replace nvals = nvals[_N] 
rename nvals DxCt15
*Collapse; max for binary, mean already counted, count bc =1 if BC visit
collapse (max) DxMentalHealth15 ReproBinary15 (mean) DxCt15, by(lopenr_mor) 
	//collapse; max for binary, mean already counted, count bc =1 if BC visit
replace DxMentalHealth15 = 0 if DxMentalHealth15==. //0s for people with no MH diag
replace DxCt15 = 0 if DxCt15==. //0s for people with no diag
replace ReproBinary15 = 0 if ReproBinary15==. //0s for people with BC visit
*Merge on to child birth year data set, and keep needed year
merge 1:m lopenr_mor using Birthyr //merge in birth year to determine age
drop _merge
keep if Byr == `yr' - 15
*Append if not the first year of data
if `yr' > 2006 append using Dx 
save Dx, replace
}
label var DxMentalHealth15 "Mom any mental health Dx at child age 15"
label var DxCt15 "Mom total unique Dx at child age 15"
label var ReproBinary15 "Mom any birth control Dx (visits)at child age 15"
*Keep needed variables
keep DxMentalHealth15 DxCt15 ReproBinary15 lopenr_mor lopenr
**Save
save MomHealthOutcomes.dta, replace
**********************************************************************************
*Fertility at age 15
use "som2008_2014.dta"
rename lopenr lopenr_mor
keep if regexm(hoved1,"Z37") | regexm(hoved2,"Z37") | ///
	regexm(hoved1,"Z38") | regexm(hoved2,"Z38") //only deliveries
// source: ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Publications/ICD10CM/2020/
gen Fertility15_ = 1
collapse (mean) Fertility15_, by(lopenr_mor aar)
reshape wide Fertility15_, i(lopenr_mor) j(aar)
merge 1:m lopenr_mor using Birthyr //merge in birth year to determine age
gen Fertility15 = .
forvalues yr=2008/2014{
replace Fertility15 = Fertility15_`yr' if Byr==`yr'-15 
}
drop Fertility15_*
drop _merge //merging again
drop if lopenr==""
*Merge birthyr again to create observation even wo fertility at age 15
merge 1:1 lopenr using Birthyr 
drop _merge
replace Fertility15 = 0 if Fertility15==. //0s for people with no admit data
label var Fertility15 "Mom fertility (admission for delivery) at age 15"
keep Fertility15 lopenr
*Merge to other health outcomes
merge 1:1 lopenr using MomHealthOutcomes.dta
drop _merge
*Save
save MomHealthOutcomes.dta, replace
**********************************************************************************
*rename all vars to have "Mom" at start
rename (DxMentalHealth15 DxCt15 ReproBinary15 Fertility15) ///
	(MomDxMentalHealth15 MomDxCt15 MomReproBinary15 MomFertility15)
*Save
save MomHealthOutcomes.dta, replace
}


































